package com.example.bumenguanli.Controller;

import com.example.bumenguanli.Service.WorkerService;
import com.example.bumenguanli.Unity.Worker;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/workers")
public class WorkerController {

    @Autowired
    private WorkerService workerService;

    // 创建工人
    @PostMapping
    public ResponseEntity<Worker> createWorker(@RequestBody Worker worker) {
        Worker createdWorker = workerService.create(worker);
        return new ResponseEntity<>(createdWorker, HttpStatus.CREATED);
    }

    // 根据ID获取工人
    @GetMapping("/{id}")
    public ResponseEntity<Worker> getWorkerById(@PathVariable Integer id) {
        Worker worker = workerService.select(id);
        return ResponseEntity.ok(worker);
    }

    @GetMapping
    public ResponseEntity<List<Worker>> getAllWorkers() {
        List<Worker> workers = workerService.selectAll();
        return ResponseEntity.ok(workers);
    }

    // 更新工人
    @PutMapping("/{id}")
    public ResponseEntity<Worker> updateWorker(@PathVariable Integer id, @RequestBody Worker worker) {
        Worker updatedWorker = workerService.update(id, worker);
        return ResponseEntity.ok(updatedWorker);
    }

    // 删除工人
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteWorker(@PathVariable Integer id) {
        workerService.delete(id);
        return ResponseEntity.noContent().build();
    }
}